capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local output "";


/* 
Combined kid and sib RD estimates
*/


local exp "prworacombsib";

local controls_all "n_male i.n_firstage i.n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar i.n_earnhhbins i.state" ;
local controls_no  "" ; 

local bwidth_list "264" ;
local cov_list   "all no" ; 
local demo_list "all male female" ; 

local vars "any* ca*" ;
local poly_list "1(1)1" ;

use "`crime'\prep2\crime_prworacombsib_prepforreg.dta", clear ;

keep if abs(dobdd_run) <= `bwidth_list' ;

/* Subsamples */
gen demo_all = 1 ;
gen byte demo_othment = (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
gen byte demo_nonment = (demo_othment == 0) ;
gen byte demo_twopar = (n_fam_singmom != 1 & n_fam_nopar != 1) ;
gen byte demo_singno = (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
gen byte demo_nopar  = (n_fam_nopar == 1) ;
gen byte demo_male 	 = (n_male ==1) ;
gen byte demo_female = (n_male !=1) ;

/* RD variables */
gen dobdd_run2=dobdd_run*dobdd_run;
gen dobdd_run3=dobdd_run2*dobdd_run;
gen dobdd_run4=dobdd_run3*dobdd_run;

gen postXdobdd=n_post*dobdd_run;
gen postXdobdd2=n_post*dobdd_run2;
gen postXdobdd3=n_post*dobdd_run3;
gen postXdobdd4=n_post*dobdd_run4;

local poly_1 "dobdd_run postXdobdd";
local poly_2 "dobdd_run dobdd_run2 postXdobdd postXdobdd2";
local poly_3 "dobdd_run dobdd_run2 dobdd_run3 postXdobdd postXdobdd2 postXdobdd3";
local poly_4 "dobdd_run dobdd_run2 dobdd_run3 dobdd_run4 postXdobdd postXdobdd2 postXdobdd3 postXdobdd4";


foreach demo in `demo_list' { ;

foreach bwidth in `bwidth_list' {;

	forval poly = `poly_list' {;
		
		foreach cov in `cov_list' { ;
		
				
				/* RD regressions */
				reg n_post n_post `poly_`poly'' 
					if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1 
					, robust;
					outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) replace;
				
				foreach var of varlist 
					`vars'
					{;
					xi: reg `var' n_post `poly_`poly''
						`controls_`cov''
						if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
						, robust; 
					outreg2 using "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_rd_`demo'.xls", e(N r2 rmse) append;
					} ;
					
		
		/* IV estimates */
			
				ivreg2 n_post (n_age18unfav=n_post) `poly_`poly''
					if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
					, robust;
					outreg2 using  "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_iv_`demo'.xls", e(N r2 rmse) replace;
					
				foreach var of varlist 
					`vars'
					{;
					
					qui summ `var' 
						if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1 ;
					if r(sd) != 0 { ;
						xi: ivreg2 `var' (n_age18unfav=n_post) `poly_`poly''
							`controls_`cov''
							if abs(dobdd_run)<=`bwidth' & demo_`demo' == 1
							, robust;
						outreg2 using  "`output'\\`exp'_bw`bwidth'_crime_poly`poly'`cov'cov_iv_`demo'.xls", e(N r2 rmse) append;
						} ;
					};
			
			} ; /* end cov */
				
		} ; /* end poly */
	} ; /* end bwidth*/
} ;	/* end demo */


capture log close;
